package com.yidu.logisticssystem.controller;

import com.yidu.logisticssystem.entity.LogisticsSection;
import com.yidu.logisticssystem.entity.vo.DepartmentVo;
import com.yidu.logisticssystem.service.LogisticsPostService;
import com.yidu.logisticssystem.service.LogisticsSectionService;
import com.yidu.logisticssystem.service.LogisticsStaffService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门(LogisticsSection)表控制层
 *
 * @author 王锦程
 * @since 2021-03-08 16:46:53
 */
@RestController
@RequestMapping("logisticsSection")
public class LogisticsSectionController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsSectionService logisticsSectionService;
    @Resource
    private LogisticsPostService logisticsPostService;
    @Resource
    private LogisticsStaffService logisticsStaffService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询")
    @GetMapping("selectOne")
    public LogisticsSection selectOne(Integer id) {
        return this.logisticsSectionService.queryById(id);
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @ApiOperation("查询所有数据")
    @GetMapping("selectAll")
    public Map<String, Object> selectAll() {
        Map<String, Object>map = new HashMap<>();
        List<LogisticsSection> list = logisticsSectionService.queryAllByLimit();
        map.put("msg","");
        map.put("code",0);
        map.put("data",list);
        return map;
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @ApiOperation("通过部门id删除")
    @GetMapping("delete")
    public Map<String, Object> delete(Integer sectionId) {
        Map<String, Object>map = new HashMap<>();
        logisticsSectionService.deleteById(sectionId);//首先删除id对应的部门
        //再删除部门下面的角色
        logisticsPostService.deleteBysectionId(sectionId);
        //再将部门下面的员工改为null
        logisticsStaffService.updateNull(sectionId);
        List<LogisticsSection> list = logisticsSectionService.queryAllByLimit();
        map.put("msg","");
        map.put("code",0);
        map.put("data",list);
        return map;
    }

    @PostMapping("findAll")
    @ApiOperation("查询部门ID和部门名称")
    public Map<String,Object> findAll(){
        Map<String, Object> map = new HashMap<>();
        List<LogisticsSection>list=logisticsSectionService.queryAllByLimit();
        map.put("data",list);
        return map;
    }

    @PostMapping("addOrUpdateSection")
    @ApiOperation("增加和修改")
    public Map<String,Object> addOrUpdateSection(LogisticsSection section){
        Map<String, Object> map = new HashMap<>();
        if (section.getSectionId()==null){
            logisticsSectionService.insert(section);
        }else {
            logisticsSectionService.update(section);
        }
        map.put("mes","操作成功");
        return map;
    }

    @GetMapping("findtree")
    @ApiOperation("查询部门ID和部门名称形成树结构")
    public List<DepartmentVo> findtree(){
        List<LogisticsSection> list = logisticsSectionService.queryAllByLimit();
        List<DepartmentVo> dms = new ArrayList<>();
        DepartmentVo paren = new DepartmentVo();
        paren.setId(0);
        paren.setName("飞鱼物流");
        paren.setTitle("飞鱼物流");
        dms.add(paren);
        for (LogisticsSection s:list) {
            DepartmentVo dm = new DepartmentVo();
            dm.setId(s.getSectionId());
            dm.setName(s.getSectionName());
            dm.setTitle(s.getSectionName());
            dm.setpId(0);
            dms.add(dm);
        }

        return dms;
    }

}
